package Zoo.View.RegisterView;

import Zoo.Handler.RegisterHandler;

import javax.swing.*;
import java.awt.*;

public class RegisterView extends JFrame /*implements ItemListener */ {
    JLabel nameLabel=new JLabel("用户注册界面",JLabel.CENTER);
    SpringLayout springLayout =new SpringLayout();
    JPanel centerPanel =new JPanel(springLayout);
    
    JLabel userNameLabel =new JLabel("用户名:");
    JTextField userTxt =new JTextField();
    
    JLabel pwdLabel=new JLabel("密码:");
    JPasswordField pwdField =new JPasswordField();
    
    JLabel phoneLabel=new JLabel("联系电话:");
    JTextField phoneTxt =new JTextField();
    
    JButton registerBtn=new JButton("注册");
    JButton resetBtn=new JButton("重置");

    RegisterHandler registerHandler;

//    private JFrame jf = new JFrame();
//    // 创建四个单选按钮
//    private JRadioButton radioBtn01 = new JRadioButton("用户");
//    private JRadioButton radioBtn02 = new JRadioButton("参观者");
//    private JRadioButton radioBtn03 = new JRadioButton("园长");
//    private JRadioButton radioBtn04 = new JRadioButton("动物饲养员");

    public RegisterView(){
        super("用户注册界面");
        registerHandler=new RegisterHandler(this);

        Container contentPane = getContentPane();

        nameLabel.setFont(new Font("华文行楷",Font.PLAIN,40));
        Font centerFont = new Font("楷体", Font.PLAIN, 20);
        userNameLabel.setFont(centerFont);
        userTxt.setPreferredSize(new Dimension(200,30));
        pwdLabel.setFont(centerFont);
        pwdField.setPreferredSize(new Dimension(200,30));
        phoneLabel.setFont(centerFont);
        phoneTxt.setPreferredSize(new Dimension(200,30));

        resetBtn.setFont(centerFont);
        registerBtn.setFont(centerFont);

        //把组件加入面板
        centerPanel.add(userNameLabel);
        centerPanel.add(userTxt);
        centerPanel.add(pwdLabel);
        centerPanel.add(pwdField);
        centerPanel.add(phoneLabel);
        centerPanel.add(phoneTxt);

        //增加按键事件
        resetBtn.addActionListener(registerHandler);
        centerPanel.add(resetBtn);
        registerBtn.addActionListener(registerHandler);
        centerPanel.add(registerBtn);

        //弹簧布局
        layourCenter();


        contentPane.add(nameLabel,BorderLayout.NORTH);
        contentPane.add(centerPanel,BorderLayout.CENTER);

        //界面大小
        setSize(600,400);
        setLocationRelativeTo(null);
        //关闭即为退出程序
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        //设置不可改变大小
        setResizable(false);
        //设置可见
        setVisible(true);

        //设置registerBtn为默认按钮
        getRootPane().setDefaultButton(registerBtn);

//        // 创建按钮组，把两个单选按钮添加到该组
//        ButtonGroup btnGroup = new ButtonGroup();
//        btnGroup.add(radioBtn01);
//        btnGroup.add(radioBtn02);
//        btnGroup.add(radioBtn03);
//        btnGroup.add(radioBtn04);
//        //单选按钮设置监听器
//        radioBtn01.addItemListener(this);
//        radioBtn02.addItemListener(this);
//        radioBtn03.addItemListener(this);
//        radioBtn04.addItemListener(this);
//        // 设置第一个单选按钮选中
//        radioBtn01.setSelected(true);
//
//        centerPanel.add(radioBtn01);
//        centerPanel.add(radioBtn02);
//        centerPanel.add(radioBtn03);
//        centerPanel.add(radioBtn04);
//        //监听窗体关闭事件
//        this.jf.addWindowListener(new WindowAdapter()
//        {
//            public void windowClosing(WindowEvent obj)
//            {
//                System.exit(1);
//            }
//        });
    }
    //

    private void layourCenter() {
        //布局userNameLabel
        Spring childWidth  =Spring.sum(Spring.sum(Spring.width(userNameLabel),Spring.width(userTxt)),Spring.constant(20));
        int offsetx=childWidth.getValue()/2;
        springLayout.putConstraint(SpringLayout.WEST,userNameLabel,-offsetx,SpringLayout.HORIZONTAL_CENTER,centerPanel);
        springLayout.putConstraint(SpringLayout.NORTH,userNameLabel,20,SpringLayout.NORTH,centerPanel);

        //布局userTxt
        springLayout.putConstraint(SpringLayout.WEST,userTxt,20,SpringLayout.EAST,userNameLabel);
        springLayout.putConstraint(SpringLayout.NORTH,userTxt,0,SpringLayout.NORTH,userNameLabel);

        //布局pwdLabel
        springLayout.putConstraint(SpringLayout.EAST,pwdLabel,0,SpringLayout.EAST,userNameLabel);
        springLayout.putConstraint(SpringLayout.NORTH,pwdLabel,20,SpringLayout.SOUTH,userNameLabel);

        //布局pwdField
        springLayout.putConstraint(SpringLayout.WEST,pwdField,20,SpringLayout.EAST,pwdLabel);
        springLayout.putConstraint(SpringLayout.NORTH,pwdField,0,SpringLayout.NORTH,pwdLabel);


        //布局phoneLabel
        springLayout.putConstraint(SpringLayout.EAST,phoneLabel,0,SpringLayout.EAST,pwdLabel);
        springLayout.putConstraint(SpringLayout.NORTH,phoneLabel,20,SpringLayout.SOUTH,pwdLabel);

        //布局phoneTxt
        springLayout.putConstraint(SpringLayout.WEST,phoneTxt,20,SpringLayout.EAST,phoneLabel);
        springLayout.putConstraint(SpringLayout.NORTH,phoneTxt,0,SpringLayout.NORTH,phoneLabel);
        
        
        
        //布局registerBtn
        springLayout.putConstraint(SpringLayout.WEST,registerBtn,50,SpringLayout.WEST,phoneLabel);
        springLayout.putConstraint(SpringLayout.NORTH,registerBtn,100,SpringLayout.SOUTH,phoneLabel);

        //布局resetBtn
        springLayout.putConstraint(SpringLayout.WEST,resetBtn,50,SpringLayout.EAST,registerBtn);
        springLayout.putConstraint(SpringLayout.NORTH,resetBtn,0,SpringLayout.NORTH,registerBtn);

//        //radioBtn01
//        springLayout.putConstraint(SpringLayout.WEST,radioBtn01,-100,SpringLayout.EAST,registerBtn);
//        springLayout.putConstraint(SpringLayout.NORTH,radioBtn01,-40,SpringLayout.NORTH,registerBtn);
//
//        //radioBtn02
//        springLayout.putConstraint(SpringLayout.WEST,radioBtn02,30,SpringLayout.EAST,radioBtn01);
//        springLayout.putConstraint(SpringLayout.NORTH,radioBtn02,0,SpringLayout.NORTH,radioBtn01);
//        //radioBtn03
//        springLayout.putConstraint(SpringLayout.WEST,radioBtn03,30,SpringLayout.EAST,radioBtn02);
//        springLayout.putConstraint(SpringLayout.NORTH,radioBtn03,0,SpringLayout.NORTH,radioBtn02);
//        //radioBtn04
//        springLayout.putConstraint(SpringLayout.WEST,radioBtn04,30,SpringLayout.EAST,radioBtn03);
//        springLayout.putConstraint(SpringLayout.NORTH,radioBtn04,0,SpringLayout.NORTH,radioBtn03);

    }

//    public static void main(String[] args){
//        new RegisterView();
//    }

    public JTextField getUserTxt() {
        return userTxt;
    }

    public JPasswordField getPwdField() {
        return pwdField;
    }

    public JTextField getPhoneTxt() {
        return phoneTxt;
    }

//    public JButton getRegisterBtn() {
//        return registerBtn;
//    }
//    @Override
//    public void itemStateChanged(ItemEvent e) {
//        if (e.getSource() == radioBtn01) {
//    //            radioBtn01.setIcon(new ImageIcon(male));
//    //            radioBtn01.setIcon(null);
//            System.out.println("用户");
//
//        } else if(e.getSource() == radioBtn02) {
//            System.out.println("参观者");
//    //            jradiofemale.setIcon(new ImageIcon(female));
//    //            jradionmale.setIcon(null);
//        }else if(e.getSource() == radioBtn03){
//            System.out.println("园长");
//        }else if(e.getSource() == radioBtn04){
//            System.out.println("动物饲养员");
//        }
//}
}
